package feizhou.untils.tools;

import javax.servlet.ServletContext;
import java.util.Map;

public class ServletContextUtil {

    /**
     * 微信开发平台的 access_token
     **/
    private static final String ACCESS_TOKEN = "access_token";

    /**
     * 微信开发平台的 jsapi_ticket
     **/
    private static final String JSAPI_TICKET = "jsapi_ticket";

    public static ThreadLocal<ServletContext> servletContextValue = new ThreadLocal<ServletContext>();

    public static void setServletContext(ServletContext servletContext) {
        servletContextValue.set(servletContext);
    }

    public static void clear() {
        servletContextValue.remove();
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> getAccessToken() {
        return (Map<String, Object>) servletContextValue.get().getAttribute(ACCESS_TOKEN);
    }

    public static void setAccessToken(Map<String, Object> accessTokenValue) {
        servletContextValue.get().setAttribute(ACCESS_TOKEN, accessTokenValue);
    }

    @SuppressWarnings("unchecked")
    public static Map<String, Object> getJsApiTicket() {
        return (Map<String, Object>) servletContextValue.get().getAttribute(JSAPI_TICKET);
    }

    public static void setJsApiTicket(Map<String, Object> JsApiTicketValue) {
        servletContextValue.get().setAttribute(JSAPI_TICKET, JsApiTicketValue);
    }
}
